from ..base_analyzer import BaseAnalyzer
from ..consts import NOT_AVAILABLE
from .. import db_helper
from ..log_helper import dlog

class LoadSoEntry(BaseAnalyzer):
    description = "SO加载: 启动全过程中所有SO加载"
    def get_result(self):
        if 'Phase1All' not in self.context:
            dlog.warning(f"Phase1All not in context: {self.context.keys()}")
            return NOT_AVAILABLE
        start_ts = self.context['Phase1All']['start_ts']
        end_candidates = []
        for phase in ['Phase4All', 'Phase3All', 'Phase2All', 'Phase1All']:
            phase_ctx = self.context.get(phase)
            if phase_ctx and phase_ctx.get('end_ts'):
                end_candidates.append(phase_ctx['end_ts'])
        end_ts = max(end_candidates, default=None)
        if end_ts is None or end_ts <= start_ts:
            dlog.warning(f"Unable to determine SO analysis range for thread {self.thread_name}")
            return NOT_AVAILABLE
        res = db_helper.get_so_load_cost(self.cursor, start_ts, end_ts, self.thread_name)
        if not res:
            return NOT_AVAILABLE
        return res
